package com.ys.peony.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.YSConstant;
import com.ys.peony.app.HttpURL;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespBean;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.VehicleModel;
import com.ys.peony.model.YWDJPrepareItemModel;
import com.ys.peony.model.YWDJPrepareModel;
import com.ys.peony.ui.adapter.DYWListViewAdapter;
import com.ys.peony.ui.view.zlistview.widget.ZListView;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by yml on 17/5/29.
 */

public class DywListActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back_btn)
    RelativeLayout backBtn;

    @BindView(R.id.title_name_tv)
    TextView titleNameTv;

    @BindView(R.id.add_btn)
    TextView addBtn;

    @BindView(R.id.dyw_lv)
    ZListView dywLV;

    private DYWListViewAdapter adapter;
    private String loadId;

    private List<VehicleModel> list = new ArrayList<VehicleModel>();

    private List<YWDJPrepareItemModel> ownerShopList = new ArrayList<YWDJPrepareItemModel>();

    @Override
    protected int getContentResId() {
        return R.layout.activity_dywlist;
    }

    @Override
    protected void initView() {
        titleNameTv.setText(getResources().getString(R.string.dyw_list_title));
        backBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);

        adapter = new DYWListViewAdapter(this);
        dywLV.setXListViewListener(new ZListView.IXListViewListener() {

            @Override
            public void onRefresh() {
                //刷新
                getDYWList();
            }

            @Override
            public void onLoadMore() {
                //加载更多
            }
        });
        dywLV.setPullLoadEnable(false);
        dywLV.setPullRefreshEnable(false);

        dywLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                LogUtil.i("aaaa", "*****dyw**position*" + position);
                //修改抵押物信息
            }

        });

        dywLV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           int position, long id) {
                return true;
            }
        });

        dywLV.setAdapter(adapter);

        Bundle bd = getIntent().getExtras();
        if (bd != null) {
            loadId = bd.getString("loanId");
        }

        getDYWList();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
//                Intent it1=new Intent();
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.add_btn:
                /*Intent it = new Intent(DywListActivity.this, AddCarActivity.class);
                it.putExtra("loanId", loadId);
                it.putExtra("position", list.size());
                it.putExtra("addFlag", true);
                startActivityForResult(it, YSConstant.ACTIVITY_ADD_YW_CLIENT);*/
                break;
        }
    }

    private void getDYWList() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }
        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Long.parseLong(SharePreferencesUtils.getInstance().getUserModel().getId()));
        params.put("loanId", loadId);

        httpManager.get(HttpURL.mortgageSee, params, new BaseRespCallBack(DywListActivity.this) {

            public void onRespSuccess(String respStr) {
                LogUtil.i("aaaa", "___list_" + respStr);
                final YWDJPrepareModel resp = (YWDJPrepareModel) JSONUtil.parseObject(respStr, YWDJPrepareModel.class);

                try {
                    JSONObject jb = new JSONObject(respStr);
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_BRANCH, jb.getJSONArray("brandList").toString());
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_OWNERSHOP, jb.getJSONArray("vehicleOwnershopTagList").toString());
                    JSONArray areaCodeArray = jb.getJSONArray("areaList");
                    JSONObject childJb = areaCodeArray.getJSONObject(0);
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_AREACODE, childJb.getJSONArray("sysAreaList").toString());
                    ownerShopList = (List<YWDJPrepareItemModel>) JSONUtil.parseArray(jb.getJSONArray("vehicleOwnershopTagList").toString(), YWDJPrepareItemModel.class);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (resp != null && resp.getBusiMortgageVo() != null) {
                    list = resp.getBusiMortgageVo().getVehicleList();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dywLV.stopRefresh();
                        dywLV.stopLoadMore();
                        adapter.setData(list, ownerShopList);
                    }
                });
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                ToastUtils.showToast(DywListActivity.this, msg);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (YSConstant.ACTIVITY_ADD_YW_CLIENT == requestCode && resultCode == RESULT_OK) {
            getDYWList();
            LogUtil.i("aaaa", "______________refresh_________");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//           Intent it=new Intent();
            setResult(RESULT_OK);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
